package com.wicket.okrbff.biz.service.impl;

import com.wicket.okrbff.biz.service.CaptchaService;
import com.wicket.okrbff.biz.service.dto.req.BffCheckCaptchaValidComReqDto;
import com.wicket.okrbff.biz.service.dto.req.BffQueryGetCaptchaDetailReqDto;
import com.wicket.okrbff.biz.service.dto.req.CheckCaptchaValidReqDto;
import com.wicket.okrbff.biz.service.dto.res.BffCheckCaptchaValidComRespDto;
import com.wicket.okrbff.biz.service.dto.res.BffQueryGetCaptchaDetailRespDto;
import com.wicket.okrbff.biz.service.dto.res.CheckCaptchaValidRespDto;
import com.wicket.okrbff.biz.service.nb.NbCaptcha;
import com.wicket.okrbff.common.util.CommonFunctionHelper;
import com.wicket.okrbff.common.util.RedisUtil;
import com.wicket.okrcomponent.integration.FwCompMessageClient;
import com.wicket.okrcomponent.integration.dto.SendVerificationCodeReqDto;
import com.wicket.okrcomponent.integration.dto.SendVerificationCodeRespDto;
import com.wicket.okrframework.integration.FwBaseBaseClient;
import com.wicket.okrframework.integration.FwBaseCaptchaClient;
import com.wicket.okrframework.integration.FwBaseUserClient;
import com.wicket.okrframework.integration.dto.QueryAppDetailReqDto;
import com.wicket.okrframework.integration.dto.QueryAppDetailRespDto;
import com.wicket.okrframework.integration.dto.QueryUserDetailReqDto;
import com.wicket.okrframework.integration.dto.QueryUserDetailRespDto;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import com.wicket.okrbff.common.exception.Assert;

/**
 * @author ：
 * @date ：
 *
 *
 * @version 1.0
 */
@Service
public class CaptchaServiceImpl implements CaptchaService {
    @Resource
    FwBaseUserClient fwBaseUserClient;
    @Resource
    FwBaseBaseClient fwBaseBaseClient;
    @Resource
    FwCompMessageClient fwCompMessageClient;
    @Resource
    FwBaseCaptchaClient fwBaseCaptchaClient;
    @Resource
    NbCaptcha nbCaptcha;
    @Resource
    private RedisUtil redisUtil;

    /**
   * B1-发送验证码（公共）[1188]
   * gen by moon at 8/10/2022, 3:33:26 AM
   */
    @Trace(operationName = "B1-发送验证码（公共）")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
  @Override
  public BffQueryGetCaptchaDetailRespDto queryGetCaptchaDetail(BffQueryGetCaptchaDetailReqDto reqDto){
      
       
       //virtualUsage D1-2查询个人账号详情  2151
      QueryUserDetailRespDto queryUserDetailRespDto = null;
    QueryUserDetailReqDto queryUserDetailReqDto=new QueryUserDetailReqDto();
  if(reqDto!=null){
      queryUserDetailReqDto.setPhone(reqDto.getPhone());//sourceId:27068_1_2151
    }
  
    /*D1-2查询个人账号详情[1032]   */
    
      queryUserDetailRespDto = fwBaseUserClient.queryUserDetail(queryUserDetailReqDto).getData();
      
      
if((queryUserDetailRespDto != null )) {
        //if(D1-2查询个人账号详情.出参 不等于空 )  2152
        
QueryAppDetailRespDto queryAppDetailRespDto = null;
    QueryAppDetailReqDto queryAppDetailReqDto=new QueryAppDetailReqDto();
  queryAppDetailReqDto.setUniqueSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID//sourceId:65166_1_2590
  
    /*D1-2查询联盟应用详情[541]   */
    
      queryAppDetailRespDto = fwBaseBaseClient.queryAppDetail(queryAppDetailReqDto).getData();
      
      
SendVerificationCodeRespDto sendVerificationCodeRespDto = null;
    if(queryAppDetailRespDto !=null){
          SendVerificationCodeReqDto sendVerificationCodeReqDto=new SendVerificationCodeReqDto();
  if(reqDto!=null){
      sendVerificationCodeReqDto.setPhone(reqDto.getPhone());//sourceId:64884_1_2591
sendVerificationCodeReqDto.setBehaviorTypeCode(reqDto.getBehaviorTypeCode());//sourceId:64885_1_2591
    }
if(queryAppDetailRespDto!=null){
      sendVerificationCodeReqDto.setAppId(queryAppDetailRespDto.getAppId());//sourceId:65179_1_2591
    }
  
    /*D2-发送短信验证码[1190]   */
    Assert.isNull(sendVerificationCodeReqDto.getPhone(),"B1-发送验证码（公共）-D2-发送短信验证码-绑定手机不能为空",false);
Assert.isNull(sendVerificationCodeReqDto.getBehaviorTypeCode(),"B1-发送验证码（公共）-D2-发送短信验证码-行为类型编码不能为空",false);
Assert.isNull(sendVerificationCodeReqDto.getAppId(),"B1-发送验证码（公共）-D2-发送短信验证码-应用ID不能为空",false);
      sendVerificationCodeRespDto = fwCompMessageClient.sendVerificationCode(sendVerificationCodeReqDto).getData();
      
      
           }
//processBranchName:正常结束 ,processBranchId:2592
        
      }
BffQueryGetCaptchaDetailRespDto retData = new BffQueryGetCaptchaDetailRespDto();
  
  

  
  
return retData;
  }

    /**
   * B1-检查验证码是否有效(公共)[2275]
   * gen by moon at 8/10/2022, 3:38:42 AM
   */
    @Trace(operationName = "B1-检查验证码是否有效(公共)")
    @Tags({@Tag(key = "参数",value = "arg[0]"),
            @Tag(key = "返回值",value = "returnedObj")})
  @Override
  public BffCheckCaptchaValidComRespDto checkCaptchaValidCom(BffCheckCaptchaValidComReqDto reqDto){
      
       
       CheckCaptchaValidRespDto checkCaptchaValidRes_1 = null;
//步骤0: D1-检测验证码是否有效（特殊方法） - checkCaptchaValid
     //ModelCode: checkCaptchaValid
        CheckCaptchaValidRespDto checkCaptchaValidRes = null;
    CheckCaptchaValidReqDto checkCaptchaValidReq=new CheckCaptchaValidReqDto();
  if(reqDto!=null){
      checkCaptchaValidReq.setPhone(reqDto.getPhone());//sourceId:66786_1
checkCaptchaValidReq.setVerificationCode(reqDto.getVerificationCode());//sourceId:66787_1
    }
  
    /*D1-检测验证码是否有效（特殊方法）[1185]  用于检测用户前端录入验证码是否有效 */
    
      checkCaptchaValidRes = nbCaptcha.checkCaptchaValid(checkCaptchaValidReq);
      
      checkCaptchaValidRes_1 = checkCaptchaValidRes;
    
BffCheckCaptchaValidComRespDto retData = new BffCheckCaptchaValidComRespDto();
  if(checkCaptchaValidRes_1!=null){
      retData.setIsInvalid(checkCaptchaValidRes_1.getIsInvalid());//sourceId:66791_1
    }
  

  
  
return retData;
  }
    //
}
